#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Create by Albert_Chen
# CopyRight (py) 2017年 陈超. All rights reserved by Chao.Chen.
# Create on 2017-02-06
# Author Albert_Chen

from __future__ import absolute_import

import time
import datetime

from celery.utils.log import get_task_logger

from .method.web_data import BaseData
from .method.webCheck import result_generator
from .method.sendMail import sendMail

logger = get_task_logger(__name__)

def check_web():

    website = BaseData.data
    start = time.time()

    logger.info('WebChecker is starting')
    results = result_generator(website)

    content = ""
    for result in results:
        message = result.get('result')
        url = result.get('url')
        if message:
            logger.info('url %s exists error %s' %(url, message))
            content += u"%s 存在 %s" %(url, message)

    if content:
        logger.info('send mail to guys')
        sendMail(['cbi28@qq.com', '1297433543@qq.com','599016585@qq.com', 'cbi65@qq.com'], '网站存在错误', content=content)

    end = time.time()
    time_cost = end - start
    logger.info('webCheck is end, time cost %5.4fs' %(time_cost))
